const path = require('path');
const os = require('os');
const webpack = require('webpack');
const HtmlHarddiskPlugin = require('html-webpack-harddisk-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyWebpackPlugin = require('uglifyjs-webpack-plugin');
const HappyPack = require('happypack');
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });

const outputDirectory = path.resolve(__dirname, '../system.console/src/main/webapp/');

const extractCSS = new ExtractTextPlugin('[name].css');

module.exports = {
    mode: 'development',
    entry: {
        main: './src/app/index.js',
        login: './src/app/login.js'
    },
    output: {
        path: outputDirectory,
        filename: '[name].min.js'
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'happypack/loader?id=happybabel'
            }
        },{
            test: /\.html$/,
            use: {
                loader: 'html-loader',
                options: {
                    attrs: ['img:data-src', 'img:src']
                }
            }
        }, {
            test: /\.css$/,
            use: extractCSS.extract({
                fallback: 'style-loader',
                use: ['css-loader']
            })
        }, {
            test: /\.less$/,
            use: extractCSS.extract({
                fallback: 'style-loader',
                use: [{
                    loader: 'css-loader',
                    options: {
                        sourceMap: true
                    }
                }, {
                    loader: 'less-loader',
                    options: {
                        sourceMap: true
                    }
                }]
            })
        }, {
            test: /\.(png|jpg|svg|gif|woff|woff2|eot|ttf)$/,
            use: {
                loader: 'file-loader',
                options: {
                    outputPath: 'src/'
                }
            }
        },{
            test: require.resolve('jquery'),
            use: [{
                loader: 'expose-loader',
                options: '$'
            }, {
                loader: 'expose-loader',
                options: 'jQuery'
            }]
        }, {
            test: require.resolve('lodash'),
            use: [{
                loader: 'expose-loader',
                options: '_'
            }]
        }]
    },
    plugins: [
        extractCSS,
        new webpack.HotModuleReplacementPlugin(),
        new UglifyWebpackPlugin({
            parallel: true,
            sourceMap: false
        }),
        new HtmlWebpackPlugin({
            title: 'easyTv',
            filename: 'index.html',
            alwaysWriteToDisk: true,
            template: 'src/index.ejs',
            chunks: ['main'],
            inject: 'body',
            minify: {
                removeComments: true
            }
        }),
        new HtmlWebpackPlugin({
            title: '用户登录',
            filename: 'login.html',
            alwaysWriteToDisk: true,
            template: 'src/login.ejs',
            chunks: ['login'],
            inject: 'body',
            minify: {
                removeComments: true
            }
        }),
        new HtmlHarddiskPlugin(),
        new HappyPack({
            id: 'happybabel',
            loaders: [{
                loader: 'babel-loader',
                options: {
                    presets: ['es2015','es2017']
                }
            }],
            threadPool: happyThreadPool,
            // cache: true,
            verbose: true
        })
    ]
};
